package com.platform.modules.sys.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.platform.common.utils.Constant;
import com.platform.common.utils.StringUtils;
import com.platform.modules.sys.dao.SysMenuDao;
import com.platform.modules.sys.entity.SysMenuEntity;
import com.platform.modules.sys.service.SysMenuService;
import com.platform.modules.sys.service.SysRoleMenuService;
import com.platform.modules.sys.service.SysUserService;

/**
 * @author 肖亮
 */
@Service("sysMenuService")
public class SysMenuServiceImpl extends ServiceImpl<SysMenuDao, SysMenuEntity> implements SysMenuService {
	@Autowired
	private SysUserService sysUserService;
	@Autowired
	private SysRoleMenuService sysRoleMenuService;
	
	@Override
	public List<SysMenuEntity> queryListParentId(String parentId, List<String> menuIdList) {
		List<SysMenuEntity> menuList = queryListParentId(parentId);
		if (menuIdList == null) {
			return menuList;
		}
		List<SysMenuEntity> userMenuList = new ArrayList<>();
		for (SysMenuEntity menu : menuList) {
			if (menuIdList.contains(menu.getMenuId())) {
				userMenuList.add(menu);
			}
		}
		return userMenuList;
	}
	
	/**
	 * 查询所有的菜单（包括二级三级菜单不包含顶级菜单）
	 * wangLc
	 */
	@Override
	public List<SysMenuEntity> queryListByCreate(Map<String, Object> params, List<String> menuIdList) {
		List<SysMenuEntity> menuList = queryMenuList(params);
		if (menuIdList == null) {
			return menuList;
		}
		List<SysMenuEntity> userMenuList = new ArrayList<>();
		for (SysMenuEntity menu : menuList) {
			if (menuIdList.contains(menu.getMenuId())) {
				userMenuList.add(menu);
			}
		}
		return userMenuList;
	}

	@Override
	public List<SysMenuEntity> queryListParentId(String parentId) {
		return baseMapper.queryListParentId(parentId);
	}

	@Override
	public List<SysMenuEntity> queryNotButtonList() {

		return baseMapper.queryNotButtonList();
	}

	@Override
	public List<SysMenuEntity> getUserMenuList(String userId) {
		// 系统管理员，拥有最高权限
		if (Constant.SUPER_ADMIN.equals(userId)) {
			return getAllMenuList(null);
		}
		Map<String, Object> params = new HashMap<String,Object>();
		params.put("parentsId", "0");//用户查询菜单时，不需要显示顶级菜单（即parentId为0的菜单不显示）
		// 普通用户菜单列表
		List<String> menuIdList = sysUserService.queryAllMenuId(userId);
//		return getAllMenu(params,menuIdList);
		return getAllMenuList(menuIdList);
	}

	@Override
	public boolean delete(String menuId) {
		// 删除菜单
		this.removeById(menuId);
		// 删除菜单与角色关联
		Map<String, Object> map = new HashMap<>(2);
		map.put("menu_id", menuId);
		return sysRoleMenuService.removeByMap(map);
	}

	@Override
	public List<SysMenuEntity> queryList() {
		return baseMapper.queryList();
	}

	@Override
	public boolean add(SysMenuEntity menu) {
		String parentId = menu.getParentId();
		String maxId="";
		if(menu.getParentId().equals("0")||menu.getParentId()==null||menu.getParentId().equals("")){
			maxId=baseMapper.queryMaxIdByParentId("0");
			int $maxId=Integer.parseInt(maxId);
			maxId=""+($maxId+1);
			menu.setMenuId(maxId);
		}else{
			maxId=baseMapper.queryMaxIdByParentId(parentId);
			menu.setMenuId(StringUtils.addOne(parentId, maxId));
		}
		return this.save(menu);
	}

	/**
	 * @descr 获取所有菜单列表
	 */
	private List<SysMenuEntity> getAllMenuList(List<String> menuIdList) {
		// 查询根菜单列表
		List<SysMenuEntity> menuList = queryListParentId("0", menuIdList);
		// 递归获取子菜单
		getMenuTreeList(menuList, menuIdList, false);

		return menuList;
	}
	
	/**
	 * @descr 获取所有父子菜单（包括父菜单和子菜单）菜单列表
	 * wangLc
	 */
	private List<SysMenuEntity> getAllMenu(Map<String, Object> params,List<String> menuIdList) {
		// 查询根菜单列表
		List<SysMenuEntity> menuList = queryListByCreate(params, menuIdList);
		// 递归获取子菜单
		getMenuTreeList(menuList, menuIdList, false);

		return menuList;
	}

	/**
	 * 获取没有按钮的菜单
	 */
	@Override
	public List<SysMenuEntity> getAllNotButtonMenuList() {
		// 查询根菜单列表
		List<SysMenuEntity> menuList = queryListParentId("-1", null);
		List<SysMenuEntity> tempMenuList = new ArrayList<>();
		for (SysMenuEntity entity : menuList) {
			entity.setUrl("");
			entity.setOpen(true);
			tempMenuList.add(entity);
		}
		// 递归获取子菜单
		getMenuTreeList(tempMenuList, null, true);
		return tempMenuList;
	}

	/**
	 * 递归
	 */
	private List<SysMenuEntity> getMenuTreeList(List<SysMenuEntity> menuList, List<String> menuIdList,
			boolean isSelect) {
		List<SysMenuEntity> subMenuList = new ArrayList<>();
		for (SysMenuEntity entity : menuList) {
			if (isSelect) {
				entity.setUrl("");
			}
			// 目录
			if (entity.getType() == Constant.MenuType.CATALOG.getValue()) {
				entity.setChildren(
						getMenuTreeList(queryListParentId(entity.getMenuId(), menuIdList), menuIdList, isSelect));
			}
			subMenuList.add(entity);
		}
		return subMenuList;
	}
	
	
	
	/**
	 * 根据where条件查询所有菜单
	 * @return List
	 */
	
	public List<SysMenuEntity> queryMenuList(Map<String, Object> params){
		return baseMapper.queryMenuList(params);
	}
	
	
}
